package com.fruit.config;

import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitKeyGenerator;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.RateLimitUtils;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.support.DefaultRateLimitKeyGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 限流配置
 * @Author: LJ
 * @Date: 2018/11/7 10:58
 * @version:
 */
@Configuration
public class RateLimiConfig {
    private static final Logger log = LoggerFactory.getLogger(RateLimiConfig.class);
    /**
     * 特定值限流
     *
     * @param properties
     * @param rateLimitUtils
     * @return
     */
    @Bean
    public RateLimitKeyGenerator ratelimitKeyGenerator(RateLimitProperties properties, RateLimitUtils rateLimitUtils) {
        return new DefaultRateLimitKeyGenerator(properties, rateLimitUtils) {
            @Override
            public String key(HttpServletRequest request, Route route, RateLimitProperties.Policy policy) {
               // System.out.println("对name=" + request.getParameter("name") + "限流");
                return super.key(request, route, policy) + ":" + request.getParameter("name");
            }
        };
    }
}
